package com.gitee.cashzhang27.test.boot.other.filter;

import com.gitee.cashzhang27.test.boot.other.entity.LogTrace;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/06/14 14:50
 */
@Slf4j
@WebFilter(urlPatterns = "/*", filterName = "logFilter")
public class LogFilter implements Filter {

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    log.info("LogFilter init successful!");
  }

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
      FilterChain filterChain) throws IOException, ServletException {
    LogTrace logTrace = new LogTrace();
    logTrace.printPreLog(servletRequest);
    filterChain.doFilter(servletRequest, servletResponse);
    logTrace.printAfterLog(servletRequest);
  }

  @Override
  public void destroy() {
    log.info("LogFilter destroy successful!");
  }
}
